package com.atguigu.service.impl;

import java.util.List;

import com.atguigu.dao.BookDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;

public class BookServiceImpl implements BookService {
	private BookDao bookDao = new BookDaoImpl();
	@Override
	public void saveBook(Book book) {
		bookDao.saveBook(book);
	}

	@Override
	public void updateBook(Book book) {
		bookDao.update(book);
	}

	@Override
	public void deleteBook(int id) {
		bookDao.deleteBook(id);
	}

	@Override
	public List<Book> queryAllBook() {
		return bookDao.queryAllBook();
	}

	@Override
	public Book queryOne(int id) {
		// TODO Auto-generated method stub
		return bookDao.queryBookOne(id);
	}

	@Override
	public Page<Book> page(int pageNo, int pageSize) {
		Page<Book> page = new Page<Book>();
		
		//在此处设置分页显示的记录
		//每页显示的数量
		page.setPageSize(pageSize);
		//获得总记录数，需要查询
		int count = bookDao.queryCount();
		page.setPageTotalCount(count);
		//设置总页码
		int pageTotal = count / page.getPageSize();
		if(count % page.getPageSize() != 0 ) {
			pageTotal ++;
		}
		page.setPageTotal(pageTotal);
		
		//设置当前页码
		page.setPageNo(pageNo);
		
		//设置当前页数据
		int begin = (page.getPageNo()-1)*page.getPageSize();
		List<Book> pageItems = bookDao.queryPageItems(begin,page.getPageSize());
		page.setItems(pageItems);
		return page;
	}

	@Override
	public Page<Book> getPageByPrice(int pageNo, int pageSize, double min, double max) {
		Page<Book> page = new Page<Book>();
		
		//在此处设置分页显示的记录
		//每页显示的数量
		page.setPageSize(pageSize);
		//获得总记录数，需要查询
		int count = bookDao.queryCountByPrice(min,max);
		page.setPageTotalCount(count);
		//设置总页码
		int pageTotal = count / page.getPageSize();
		if(count % page.getPageSize() != 0 ) {
			pageTotal ++;
		}
		page.setPageTotal(pageTotal);
		
		//设置当前页码
		page.setPageNo(pageNo);
		
		//设置当前页数据
		int begin = (page.getPageNo()-1)*page.getPageSize();
		List<Book> pageItems = bookDao.queryPageItemsByPrice(begin,page.getPageSize(),min,max);
		page.setItems(pageItems);
		return page;
	}

}
